﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Mondial2014.Models.Context;
using Mondial2014.Models.Gambling;

namespace Mondial2014.Controllers
{
    public class GameGamblesController : Controller
    {
        private readonly Mondial2014DbContext m_Context = new Mondial2014DbContext();

        private const int GroupCount = 8;

        public class GameGambleViewModel
        {
            public int GambleId { get; set; }
            public int GamblerId { get; set; }
            public string GamblerName { get; set; }
            public int GameNumber { get; set; }
            public DateTime Date { get; set; }
            public string Team1 { get; set; }
            public string Team2 { get; set; }
            public GameGambleResult Result { get; set; }
        }

        // GET: GameGambles
        public ActionResult Index(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var gambler = m_Context.Gamblers.Find(id);
            if (gambler == null)
            {
                return HttpNotFound();
            }

            var list = new List<GameGambleViewModel>();

            for (var i = 0; i <= GroupCount; i++)
            {
                foreach (var fixture in m_Context.GetFixtures().Where(f => f.Stage.Level == 0).ToArray())
                {
                    var gameGamble = m_Context.GameGambles.SingleOrDefault(
                        gg => gg.Fixture.Number == fixture.Number && gg.Gambler.Id == gambler.Id) ??
                            m_Context.GameGambles.Add(new GameGamble
                            {
                                Gambler = m_Context.Gamblers.Single(g => g.Id == gambler.Id),
                                Fixture = m_Context.GetFixtures().Single(f => f.Number == fixture.Number)
                            });
                    list.Add(new GameGambleViewModel
                    {
                        GambleId = gameGamble.Id,
                        GamblerId = gambler.Id,
                        GamblerName = gambler.Name,
                        Date = gameGamble.Fixture.Date,
                        GameNumber = gameGamble.Fixture.Number,
                        Team1 = gameGamble.Fixture.Team1.Name,
                        Team2 = gameGamble.Fixture.Team2.Name,
                        Result = gameGamble.GameGambleResult
                    });
                }
            }

            m_Context.SaveChanges();

            return View(list);
        }

        [HttpPost]
        public ActionResult Index(List<GameGambleViewModel> gameGambleViewModels)
        {
            foreach (var gameGambleViewModel in gameGambleViewModels)
            {
                var gamble = m_Context.GameGambles.Find(gameGambleViewModel.GambleId);
                gamble.GameGambleResult = gameGambleViewModel.Result;
            }

            m_Context.SaveChanges();

            return RedirectToAction("Index", new { id = gameGambleViewModels.First().GamblerId });
        }
    }
}